	package it.asvp.asvp_portal.web.controller;

import it.asvp.asvp_portal.asvp_util.util.Constants;
import it.asvp.asvp_portal.data.entity.domain.Convention;
import it.asvp.asvp_portal.service.services.ConventionService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/convention")
public class ConventionController extends BaseController{

	private static final Log log = LogFactory.getLog(ConventionController.class);
	
	@Autowired
	ConventionService conventionService;
	
	@RequestMapping(value = "/default", method = RequestMethod.GET)
	public ModelAndView getAll(final HttpServletRequest request, HttpServletResponse response) {
    
		log.info("Accesso alla pagina di tutte le convenzioni");
		
		try{
			
			ModelAndView mv = getModelAndView("convention/default");
		    mv.addObject(Constants.Convention.CONVENTIONS, conventionService.getAllConventions());
		    return mv;
		    
	    }catch (Exception e) {
		   return getErrorModelAndView(e);
	    }
	}
	
	@RequestMapping(value = "/insert", method = RequestMethod.GET)
	public ModelAndView insert(@ModelAttribute("convention")Convention convention,  BindingResult result,HttpServletResponse response) {

		log.info("Accesso alla form di inserimento di una nuova convenzione");
	    try{	
	    	
	       //valore di default per active
	       convention.setActive(false);
	       ModelAndView mv = getModelAndView("convention/insert");
		   mv.addObject(Constants.Convention.CONVENTION,convention );
		   return mv;	
		   
	    }catch (Exception e) {
		   return getErrorModelAndView(e);
	    }
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView save(HttpServletRequest request,@ModelAttribute("command")Convention newConvention,  BindingResult result) {

		log.info("Accesso alla pagina di salvataggio di una nuova convenzione");
		try{
			
			String active=request.getParameter("active");
			if("true".equals(active)){
				newConvention.setActive(true);
			}else{
				newConvention.setActive(false);
			}
			//TODO:validazione del form
			//FIXME: 1L=id utente per i test. verra sostituito dall'id dell'utente loggato
		   conventionService.createConvention(getUserDetail(), newConvention);
		   return new ModelAndView(Constants.REDIRECT+"/convention/default");
		   
	    }catch (Exception e) {
		   return getErrorModelAndView(e);
	    }
	}
	
	@RequestMapping(value = "/update/{idConvention}", method = RequestMethod.POST)
	public ModelAndView update(@PathVariable final long idConvention,Convention newDataConvention,final HttpServletRequest request) {

		log.info("Accesso alla pagina di aggiornamento della convenzione con id="+idConvention);
		
	  try{
		  
		 conventionService.updateConvention(getUserDetail(), idConvention, newDataConvention);
		 return new ModelAndView(Constants.REDIRECT+"/convention/default");
		 
	  } catch (Exception e) {
		 return getErrorModelAndView(e);
	  }
	}
	
	@RequestMapping(value = "/remove/{idConvention}", method = RequestMethod.POST)
	public ModelAndView remove(@PathVariable final long idConvention, final HttpServletResponse response) {

		log.info("Accesso alla cacellazione della convenzione con id="+idConvention);
		
		try {
			
			conventionService.deleteConvention(getUserDetail(), idConvention);
		    return new ModelAndView(Constants.REDIRECT+"/convention/default");	
		    
		} catch (Exception e) {
			return getErrorModelAndView(e);
		}
	}
	
	@RequestMapping(value = "/edit/{idConvention}", method = RequestMethod.GET)
	public ModelAndView edit(@PathVariable final Long idConvention, final HttpServletResponse response) {

		log.info("Accesso alla pagina di visualizzazione della convenzione con id="+idConvention);
		
		try {
			
			ModelAndView mv = getModelAndView("convention/edit");
			mv.addObject(Constants.Convention.CONVENTION, conventionService.getById(idConvention));
			return mv;	
			
		} catch (Exception e) {
			return getErrorModelAndView(e);
		}
	}
}
